<template>
	<view class="listcontent container">
		<view class="contelis">
			<view class="titles">
				编辑博客
				<view class="backs" @click="backgs"> <返回</view>
			</view>
			<ivew class="titleses">
				标题：<input class="uni-input" v-model="titles" focus placeholder="请输入标题" />
				<u-button @click="show = true" style="float: right;margin-right: 20px; height: 30px; line-height: 30px; font-size: 14px;">{{label}}</u-button>
			</ivew>
			<view class="addclass">
				<view class='toolbar' @tap="format" style="overflow-y: auto;">
					<view :class="formats.bold ? 'ql-active' : ''" class="iconfont icon-zitijiacu" data-name="bold"></view>
					<view :class="formats.italic ? 'ql-active' : ''" class="iconfont icon-zitixieti" data-name="italic"></view>
					<view :class="formats.underline ? 'ql-active' : ''" class="iconfont icon-zitixiahuaxian" data-name="underline"></view>
					<view :class="formats.strike ? 'ql-active' : ''" class="iconfont icon-zitishanchuxian" data-name="strike"></view>
					<view :class="formats.align === 'left' ? 'ql-active' : ''" class="iconfont icon-zuoduiqi" data-name="align"
					 data-value="left"></view>
					<view :class="formats.align === 'center' ? 'ql-active' : ''" class="iconfont icon-juzhongduiqi" data-name="align"
					 data-value="center"></view>
					<view :class="formats.align === 'right' ? 'ql-active' : ''" class="iconfont icon-youduiqi" data-name="align"
					 data-value="right"></view>
					<view :class="formats.align === 'justify' ? 'ql-active' : ''" class="iconfont icon-zuoyouduiqi" data-name="align"
					 data-value="justify"></view>
					<view :class="formats.lineHeight ? 'ql-active' : ''" class="iconfont icon-line-height" data-name="lineHeight"
					 data-value="2"></view>
					<view :class="formats.letterSpacing ? 'ql-active' : ''" class="iconfont icon-Character-Spacing" data-name="letterSpacing"
					 data-value="2em"></view>
					<view :class="formats.marginTop ? 'ql-active' : ''" class="iconfont icon-722bianjiqi_duanqianju" data-name="marginTop"
					 data-value="20px"></view>
					<view :class="formats.previewarginBottom ? 'ql-active' : ''" class="iconfont icon-723bianjiqi_duanhouju" data-name="marginBottom"
					 data-value="20px"></view>
					<view class="iconfont icon-clearedformat" @tap="removeFormat"></view>
					<view :class="formats.fontFamily ? 'ql-active' : ''" class="iconfont icon-font" data-name="fontFamily" data-value="Pacifico"></view>
					<view :class="formats.fontSize === '24px' ? 'ql-active' : ''" class="iconfont icon-fontsize" data-name="fontSize"
					 data-value="24px"></view>
				
					<view :class="formats.color === '#0000ff' ? 'ql-active' : ''" class="iconfont icon-text_color" data-name="color"
					 data-value="#0000ff"></view>
					<view :class="formats.backgroundColor === '#00ff00' ? 'ql-active' : ''" class="iconfont icon-fontbgcolor"
					 data-name="backgroundColor" data-value="#00ff00"></view>
				
					<view class="iconfont icon-date" @tap="insertDate"></view>
					<view class="iconfont icon--checklist" data-name="list" data-value="check"></view>
					<view :class="formats.list === 'ordered' ? 'ql-active' : ''" class="iconfont icon-youxupailie" data-name="list"
					 data-value="ordered"></view>
					<view :class="formats.list === 'bullet' ? 'ql-active' : ''" class="iconfont icon-wuxupailie" data-name="list"
					 data-value="bullet"></view>
					<view class="iconfont icon-undo" @tap="undo"></view>
					<view class="iconfont icon-redo" @tap="redo"></view>
				
					<view class="iconfont icon-outdent" data-name="indent" data-value="-1"></view>
					<view class="iconfont icon-indent" data-name="indent" data-value="+1"></view>
					<view class="iconfont icon-fengexian" @tap="insertDivider"></view>
					<view class="iconfont icon-charutupian" @tap="insertImage"></view>
					<view :class="formats.header === 1 ? 'ql-active' : ''" class="iconfont icon-format-header-1" data-name="header"
					 :data-value="1"></view>
					<view :class="formats.script === 'sub' ? 'ql-active' : ''" class="iconfont icon-zitixiabiao" data-name="script"
					 data-value="sub"></view>
					<view :class="formats.script === 'super' ? 'ql-active' : ''" class="iconfont icon-zitishangbiao" data-name="script"
					 data-value="super"></view>
					<view class="iconfont icon-shanchu" @tap="clear"></view>
					<view :class="formats.direction === 'rtl' ? 'ql-active' : ''" class="iconfont icon-direction-rtl" data-name="direction"
					 data-value="rtl"></view>
				
				</view>
				
			</view>
			<view class="addblog">
				<editor id="editor" class="ql-container" placeholder="开始输入..." showImgSize showImgToolbar showImgResize
				 @statuschange="onStatusChange" :read-only="readOnly" @ready="onEditorReady" @blur="getContents">
				</editor>
			</view>
		    <view class="butos">
				<button type="primary" style="width: 150px;"  @click="sendtoroots">修改</button>
				<!-- <button type="warn"  style="width: 150px;" @click="resets">重置</button> -->
			</view>
		</view>
		<u-select v-model="show" :list="clists" @confirm="confirm"></u-select>
		<u-modal v-model="shows" @confirm="confirm1" style="text-align: center; padding: 20px 0;" :show-cancel-button="true" >确定提交吗？</u-modal>
	</view>
</template>

<script>
	import api from '../../api/api.js';
	import {mapGetters,mapMutations} from 'vuex';
	export default {
		data() {
			return {
				readOnly: false,
				formats: {},
				htmls:'',
				titles:'',
				cid:'',
				Id:'',
				show:false,
				shows:false,
				label:'分类',
				clists:[]
			}
		},
		computed: {
			...mapGetters(['editedataz'])
		},
		onLoad() {
			this.onEditorReady();
		},
		onShow() {
		   console.log('222',this.editedataz);
		   const _this = this;
		   this.shows = false;
		   if(this.editedataz){
			    this.Id = this.editedataz.Id;
			   	this.cid=this.editedataz.cid;
			   	this.label = this.editedataz.cname;
			   	this.titles=this.editedataz.title;
			    this.onEditorReady();
		   }
		},
		methods: {
			resets(){
				const _this = this;
				_this.cid='';
				_this.label = '分类';
				_this.titles='';
				uni.createSelectorQuery().select('#editor').context((res) => {
					_this.editorCtx.setContents({//赋值
						  html:''
						 });
				}).exec()
			},
			backgs(){
				
						uni.navigateTo({
							url:'/pages/index/index'
						})
			},
			sendtoroots(){
				this.shows = !this.shows;
			},
			confirm1(){
				if(!this.titles || !this.htmls){
					uni.showToast({
						title:"请填写标题和内容",
						icon: 'none'
					});
					return
				}
				if(!this.cid){
					uni.showToast({
						title:"请选择分类",
						icon: 'none'
					});
					return
				}
				const datas = {
					Id:this.Id,
					title:this.titles,
					content:this.htmls,
					cid:Number(this.cid),
					uid:uni.getStorageSync('uid')
				}
				this.shows = !this.shows;
				api.editeblogs(datas).then((res)=>{
					console.log(res);
					uni.navigateTo({
						url:"/pages/manage/index"
					})
				})
			},
			getContents(){
				const _this = this;
				_this.editorCtx.getContents({
					success(res) {
						_this.htmls = res.html
					}
				})
				console.log(this.htmls);
			},
			readOnlyChange() {
				this.readOnly = !this.readOnly
			},
			onEditorReady() {
				const _this = this;
				uni.createSelectorQuery().select('#editor').context((res) => {
					this.editorCtx = res.context
					this.editorCtx.setContents({    
							html:_this.editedataz.content    //this.EditGoodsDetail.content为赋值内容。    
					})  
				}).exec()
				this.content= _this.editedataz.content;
			},
			undo() {
				this.editorCtx.undo()
			},
			redo() {
				this.editorCtx.redo()
			},
			format(e) {
				let {
					name,
					value
				} = e.target.dataset
				if (!name) return
				// console.log('format', name, value)
				this.editorCtx.format(name, value)
		
			},
			onStatusChange(e) {
				const formats = e.detail
				this.formats = formats
			},
			insertDivider() {
				this.editorCtx.insertDivider({
					success: function() {
						console.log('insert divider success')
					}
				})
			},
			clear() {
				this.editorCtx.clear({
					success: function(res) {
						console.log("clear success")
					}
				})
			},
			removeFormat() {
				this.editorCtx.removeFormat()
			},
			insertDate() {
				const date = new Date()
				const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
				this.editorCtx.insertText({
					text: formatDate
				})
			},
			insertImage() {
				let that = this;
				uni.chooseImage({
					count: 1,
					success: (res) => {
						//上传文件的临时路径
						const tempFilePaths = res.tempFilePaths;
						uni.uploadFile({
							url: 'http://127.0.0.2:8188/index.php/index/index/upload',
							filePath: tempFilePaths[0],
							name: 'file',
							method:'POST',
							cache: false,
				　　　　　　 processData: false,
				　　　　　　 contentType: false,
							header: {
								"token": uni.getStorageSync('token'),
								//"Content-Type": "multipart/form-data;charset=utf-8;boundary=100"
							},
							success: (uploadFileRes) => {
								console.log("编辑详情的时候返回照片地址")
								// console.log(uploadFileRes)
								uni.hideLoading();
								const back = JSON.parse(uploadFileRes.data);
								console.log('back',back);
								if(back.code==1){
									console.log('http://127.0.0.2:8188'+back.path)
									that.editorCtx.insertImage({
										src: 'http://127.0.0.2:8188'+back.path,
										alt: '图像',
										success: function() {
										}
									})
								}else{
									that.showToast(back.msg)
								}
							},
							fail:() =>{
								uni.hideLoading();
								that.showToast("图片上传失败，请联系开发！")
							}
						});
					}
				})
			},
			
			
			
			confirm(obj){
				console.log('obj',obj);
				this.label = obj[0].label;
				this.cid = obj[0].value;
				console.log(this.titles);
				console.log(this.htmls);
			},
			autoplays(){
				api.clist().then((res)=>{
					console.log(res);
					res.data.forEach((item)=>{
						this.clists.push({
							value: item.Id,
							label: item.cname
						})
					})
				})
			},
		},
		onLoad() {
			this.autoplays();
			uni.loadFontFace({
				family: 'Pacifico',
				source: 'url("https://sungd.github.io/Pacifico.ttf")'
			})
		},
	}
</script>

<style>
	@import "./editor-icon.css";
	.listcontent{background: #27ae60; width: 100%; overflow-y:scroll; height: 100vh; ;}
	.contelis{ width: 900px; margin: 0 auto; background-color: #fff; height: 100vh; overflow-y: scroll;}
	.titles{  background-color: #ccc; height: 150px; line-height: 150px;text-align: center; font-size: 20px; position: relative;}
	.backs{ position: absolute; left: 50px; top: 20px; font-size: 18px; height: 30px; line-height: 30px;}
	.addblog{ background-color: #eee; width:100%; min-height: 200px;}
	#editor {width: 100%;height: 300px;background-color: #CCCCCC;}
	.wrapper {
		height: 100%;
	}
	
	.editor-wrapper {
		height: calc(100vh - var(--window-top) - var(--status-bar-height) - 140px);
		background: #fff;
	}
	
	.iconfont {
		display: inline-block;
		padding: 8px 8px;
		width: 24px;
		height: 24px;
		cursor: pointer;
		font-size: 20px;
	}
	
	.toolbar {
		box-sizing: border-box;
		border-bottom: 0;
		font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
	}
	
	
	.ql-container {
		box-sizing: border-box;
		padding: 12px 15px;
		width: 100%;
		min-height: 30vh;
		height: 100%;
		margin-top: 20px;
		font-size: 16px;
		line-height: 1.5;
	}
	
	.ql-active {
		color: #06c;
	}
    .titleses{ display: flex; align-items: center; height: 50px; line-height: 50px; font-size: 16px;}
	.butos{ display: flex; padding: 30px 0;}
</style>
